import QtQuick 2.12
import QtQuick.Window 2.12
import QtQml.Models 2.12
import QtQuick.Controls 2.12
import Qt.labs.qmlmodels 1.0
import FileHandler 1.0

Rectangle {
    width: parent.width
    height: parent.height
//    border.color: "black"


    Row {
        id: titleRow
        width: parent.width
        height: unit
        Rectangle {
            id: rectangle
            color: "#e0e0e0"
            width: unit*3
            height: unit
            Text{
                anchors.centerIn: parent
                font.pointSize: 10
                text:"文件名"
            }
        }
        Rectangle {
            id: rectangle1
            width: unit*4
            height: unit
            color: "#e0e0e0"
            Text{
                anchors.centerIn: parent
                font.pointSize: 10
                text:"进度条"
            }
        }
        Rectangle {
            id: rectangle2
            width: unit*2
            height: unit
            color: "#e0e0e0"
            Text{
                anchors.centerIn: parent
                font.pointSize: 10
                text:"速 度"
            }
        }
        Rectangle {
            id: rectangle3
            color: "#e0e0e0"
            Text{
                anchors.centerIn: parent
                font.pointSize: 10
                text:"接收/发送"
            }
            width: unit*2
            height: unit

        }
        Rectangle {
            id: rectangle4
            color: "#e0e0e0"
            Text{
                anchors.centerIn: parent
                font.pointSize: 10
                text:"对方设备名"
            }
            width: unit*3
            height: unit
        }
    }

    Row {
        id: fileList
        anchors.top: titleRow.bottom
        width: parent.width
        height: parent.height - unit

        ListModel {
            id: myModel

        }

        ListView {
            id: list
            model: myModel
            width: parent.width
            height: parent.height
            spacing: 2
            delegate: RecvRow {
                hUnit: unit
                magicNum: gMagic
                fileName: gFileName
                len: gLen
                rate: gRate
                type: gType
                deviceName: gDeviceName
            }
        }
    }
    Connections{
        target: FileHandler
        function onGetFileName(magic,filename){
            //执行一个function修改文件名
            updateFileName(magic,filename)
        }
        function onFirstFileType(magic, fileName, type){
            //执行一个function判断类型，只执行一次
            updateType(magic, fileName, type)
        }

        function onUpdateRatio(magic,ratio,type){
            //执行一个function修改进度
            updateRatio(magic,ratio,type)
        }
        function onUpdateRate(magic,rate,type){
            //执行一个function修改速度
            updateRate(magic,rate,type)
        }
        function onDeleteFileList(magic){
            //将其从文件列表中移除
            removeFromModel(magic)
        }
    }
    function updateFileName(magic,filename){
        console.log("==========>>update filename")
        for(let i = 0;i<myModel.count;i++){
            if(myModel.get(i).gMagic === magic){
                myModel.get(i).gFileName = filename
                name = filename
                 console.log("filename update success!!!")
                break;
            }
        }
    }
    //当接收到一份新文件的时候就append到model里面
//    property var flage: 1
//    property var name: ""
    function updateType(magic, fileName, type){
        if (fileName.length > 8) {
            fileName = fileName.substr(0, 8) + '...'
        }
        console.log("==========>>update Type")
        if(type === 1){
            let data = {
                gMagic: magic,
                gFileName: fileName,
                gLen:0,
                gRate:0,
                gType:"发送",
                gDeviceName:"设备0"
            }
            myModel.append(data)
        }
        else if(type === 0){
            let data = {
                gMagic: magic,
                gFileName: fileName,
                gLen:0,
                gRate:0,
                gType:"接收",
                gDeviceName:"设备0"
            }
            myModel.append(data)
        }
    }
    function updateRatio(magic,ratio,type){
        console.log("===>>ratio update")
        let rType = type === 1 ? "发送" : "接收"
        for(let i = 0;i<myModel.count;i++){
            if(myModel.get(i).gMagic === magic && myModel.get(i).gType === rType)
            {
                myModel.get(i).gLen = ratio;
                console.log(magic+"ratio update success!!!")
//                break;
            }
        }
    }
    function updateRate(magic,rate,type) {
        console.log("=======>rate update")
//        console.log('aaaaaaaaaaaaa',rate)
        let rType = type === 1 ? "发送" : "接收"
        for(let i = 0;i<myModel.count;i++){
            if(myModel.get(i).gMagic === magic && myModel.get(i).gType === rType)
            {
//                console.log('bbbbbbbbbbbbbb', (rate / 1024 / 1024).toFixed(2))
                myModel.get(i).gRate = (rate / 1024 / 1024).toFixed(2) * 1
                console.log(magic+"rate update success!!!")
//                break;
            }
        }
    }
    function removeFromModel(magic){
        console.log("remove from model")
//        console.log(magic)
        for (let i = myModel.count - 1; i >= 0; i --) {
            if (myModel.get(i).gMagic === magic) {
                myModel.remove(i);
                console.log("remove success!!!")
//                break;
            }
        }
    }
}
